# -*- coding: mbcs -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *
import math

##-----------------------------------------------
session.journalOptions.setValues(replayGeometry=COORDINATE, recoverGeometry=COORDINATE)
##-----------------------------------------------
Mdb()

## =======
## DATA
## =======



TH = 0.23
DIA = 10.0
##Remember to change geometry

############################################################
#CREATE DISK GEOMETRY
############################################################
mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=1.0)
mdb.models['Model-1'].sketches['__profile__'].CircleByCenterPerimeter(center=(
    0.0, 0.0), point1=(0.0, DIA/2))
mdb.models['Model-1'].Part(dimensionality=THREE_D, name = 'Disk', 
    type=DEFORMABLE_BODY)
mdb.models['Model-1'].parts['Disk'].BaseSolidExtrude(depth = TH, 
    sketch=mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']
S = 0.25 #seeding size for analysis = 0.25

## =======
## Mesh the plate
## =======
mdb.models['Model-1'].parts['Disk'].setElementType(elemTypes=(ElemType(
    elemCode=C3D8, elemLibrary=STANDARD, secondOrderAccuracy=OFF, 
    kinematicSplit=AVERAGE_STRAIN, hourglassControl=DEFAULT, 
    distortionControl=DEFAULT), ElemType(elemCode=C3D6, elemLibrary=STANDARD), 
    ElemType(elemCode=C3D4, elemLibrary=STANDARD)), regions=(
    mdb.models['Model-1'].parts['Disk'].cells.findAt(((1.0, 0.666667, 
    0.02), )), ))

mdb.models['Model-1'].parts['Disk'].seedPart(deviationFactor=0.1, 
    minSizeFactor=0.1, size=S)
mdb.models['Model-1'].parts['Disk'].generateMesh()


## ================== Determine director vector per element, originally unitvecs.txt
f = open('unitvecs.txt','r')
unitvecs = f.readlines()


mdb.models['Model-1'].rootAssembly.Instance(dependent=ON, name='Disk-1', part=
    mdb.models['Model-1'].parts['Disk'])

m = 0
n = 1
mat = 0
sect = 0
setn = 0
for i in range(0,len(unitvecs)):

    lin = unitvecs[i].split()
    a = float(lin[0])
    b = float(lin[1])
    c = float(lin[2])
    nmat = str(mat)
    nsec = str(sect)
    snam = str(setn)
#Create material using a b and c per element
    mdb.models['Model-1'].Material(name= 'mat'+nmat)
    mdb.models['Model-1'].materials['mat'+nmat].Depvar(n=5)
    mdb.models['Model-1'].materials['mat'+nmat].UserMaterial(mechanicalConstants=
    (3.4, 0.49, a,b,c))
    mdb.models['Model-1'].HomogeneousSolidSection(material='mat'+nmat, name=
        'sect'+nsec, thickness=None)
    
    mdb.models['Model-1'].parts['Disk'].Set(elements=
    mdb.models['Model-1'].parts['Disk'].elements[m:n], name='set'+snam)
    
    
    
    #Assign material to section
    mdb.models['Model-1'].parts['Disk'].SectionAssignment(offset=0.0, offsetField=
        '', offsetType=MIDDLE_SURFACE, region=
    mdb.models['Model-1'].parts['Disk'].sets['set'+snam], sectionName=
        'sect'+nsec, thicknessAssignment=FROM_SECTION)
    m = m+1
    n = n+1
    mat = mat+1
    sect = sect +1
    setn = setn+1
  

## =======
## LCE MATERIAL - 5 variables: 
# 1st Young's Modulus i.e. 1.0
# 2nd Poissons' Ratio i.e. 0.49
# 3rd-4th-5th director cosines of the nematic vector i.e. a,b,c
## =======

f.close()

## =======
## Definition of static step
## =======

mdb.models['Model-1'].StaticStep(initialInc=0.001, maxInc=0.01, maxNumInc=10000000000000, 
    minInc=1e-23, name='Traction', nlgeom=ON, previous='Initial')

mdb.models['Model-1'].rootAssembly.DatumCsysByDefault(CARTESIAN)

mdb.models['Model-1'].rootAssembly.Instance(dependent=ON, name='Disk-1', part=
    mdb.models['Model-1'].parts['Disk'])

## =======
## Apply constraints on a central element, here I just create the set I forgot to add this part into the script so I just create the BC in the CAE
## desirable nodes are last nodes (ex. node1 = 0:1)
## =======

#seed = 0.5
'''
mdb.models['Model-1'].parts['Disk'].Set(name='center_nodes', nodes=
    mdb.models['Model-1'].parts['Disk'].nodes[881:882]+\
    mdb.models['Model-1'].parts['Disk'].nodes[994:995]+\
    mdb.models['Model-1'].parts['Disk'].nodes[891:892]+\
    mdb.models['Model-1'].parts['Disk'].nodes[895:896]+\
    mdb.models['Model-1'].parts['Disk'].nodes[383:384]+\
    mdb.models['Model-1'].parts['Disk'].nodes[496:497]+\
    mdb.models['Model-1'].parts['Disk'].nodes[393:394]+\
    mdb.models['Model-1'].parts['Disk'].nodes[397:398])
'''
''' seed = ??? (I don't remember)
mdb.models['Model-1'].parts['Disk'].Set(name='center_nodes', nodes=
    mdb.models['Model-1'].parts['Disk'].nodes[3031:3032]+\
    mdb.models['Model-1'].parts['Disk'].nodes[3341:3342]+\
    mdb.models['Model-1'].parts['Disk'].nodes[3349:3350]+\
    mdb.models['Model-1'].parts['Disk'].nodes[3023:3024]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1355:1356]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1665:1666]+\
    mdb.models['Model-1'].parts['Disk'].nodes[167]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1347:1348])
'''

#seed = 0.25
mdb.models['Model-1'].parts['Disk'].Set(name='center_nodes', nodes=
    mdb.models['Model-1'].parts['Disk'].nodes[3348:3349]+\
    mdb.models['Model-1'].parts['Disk'].nodes[3030:3031]+\
    mdb.models['Model-1'].parts['Disk'].nodes[3024:3025]+\
    mdb.models['Model-1'].parts['Disk'].nodes[3023:3024]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1672:1673]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1354:1355]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1348:1349]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1347:1348])

'''
wrong dims
mdb.models['Model-1'].parts['Disk'].Set(name='center_nodes', nodes=
    mdb.models['Model-1'].parts['Disk'].nodes[744:745]+\
    mdb.models['Model-1'].parts['Disk'].nodes[751:752]+\
    mdb.models['Model-1'].parts['Disk'].nodes[752:753]+\
    mdb.models['Model-1'].parts['Disk'].nodes[754:755]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1647:1648]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1654:1655]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1655:1656]+\
    mdb.models['Model-1'].parts['Disk'].nodes[1657:1658])
    
  '''  

## =======
## Ask for some specific output
## =======

mdb.models['Model-1'].fieldOutputRequests['F-Output-1'].setValues(numIntervals=
    100)
mdb.models['Model-1'].fieldOutputRequests['F-Output-1'].setValues(
    variables=('S', 'E', 'U', 'UR', 'RF'))


mdb.Job(atTime=None, contactPrint=OFF, description='', echoPrint=OFF, 
    explicitPrecision=SINGLE, getMemoryFromAnalysis=True, historyPrint=OFF, 
    memory=90, memoryUnits=PERCENTAGE, model='Model-1', modelPrint=OFF, 
    multiprocessingMode=DEFAULT, name='Disk_splay', nodalOutputPrecision=SINGLE, 
    numCpus=1, numGPUs=0, queue=None, scratch='', type=ANALYSIS, 
    userSubroutine=
    '/n/home12/mwilborn/umatNum_GL_Liquid_Crystal_v4_coupling_v5_alpha0_0200.f'
    , waitHours=0, waitMinutes=0)
